package org.smallreunion.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.Model;
import org.smallreunion.model.constract.CounterShardType;

/**
 * CounterShard
 *
 * @author Sinmetal
 *
 */
@Model(schemaVersion = 1)
public class CounterShard implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final int startKeyNumber = 1;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;

    private CounterShardType name;

    @Attribute(unindexed = true)
    private Integer count;

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    /**
     * @return name
     */
    public CounterShardType getName() {
        return name;
    }

    /**
     * @param name
     *            セットする name
     */
    public void setName(CounterShardType name) {
        this.name = name;
    }

    /**
     * @return count
     */
    public Integer getCount() {
        return count;
    }

    /**
     * @param count
     *            セットする count
     */
    public void setCount(Integer count) {
        this.count = count;
    }

    /**
     * カウンタをインクリメント
     */
    public void increment() {
        this.count++;
    }

    /**
     * Key生成
     *
     * @param name
     * @param numShard
     * @return
     */
    public static Key createKey(CounterShardType name) {
        Random random = new Random();
        int number = random.nextInt(name.getNumShards()) + startKeyNumber;
        return createKey(name, number);
    }

    public static Key createKey(CounterShardType name, int number) {
        String id = String.format("%s%d", name, number);
        return Datastore.createKey(CounterShard.class, id);
    }

    /**
     * インスタンス生成
     *
     * @return
     */
    public static CounterShard getInstance(CounterShardType name) {
        CounterShard counterShard = new CounterShard();
        counterShard.count = 0;
        counterShard.name = name;
        return counterShard;
    }

    /**
     * Key一覧取得
     *
     * @param name
     * @return
     */
    public static List<Key> getKeyList(CounterShardType name) {
        List<Key> keys = new ArrayList<Key>();
        for (int i = 0; i < name.getNumShards(); i++) {
            keys.add(createKey(name, i + startKeyNumber));
        }
        return keys;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        CounterShard other = (CounterShard) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}
